package ttc.sftp.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class DownloadService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Toast.makeText(getApplicationContext(), "create service", Toast.LENGTH_SHORT).show();
		
		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Toast.makeText(getApplicationContext(), "service destroy", Toast.LENGTH_SHORT).show();
	}
	

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		
		String username = intent.getStringExtra("usernameKey");
		String password = intent.getStringExtra("passwordKey");
		String host = intent.getStringExtra("hostKey");
		String portSent = intent.getStringExtra("portKey");
		int port =Integer.parseInt(portSent);		
		String filePath = intent.getStringExtra("filePathKey");
		String filename = intent.getStringExtra("folderClickedKey");
				
	    
		
		
		
		try {
			download(username, password, host, port, filePath, filename);
			Toast.makeText(getApplicationContext(),"download completed", Toast.LENGTH_LONG).show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	
	
	
	public void download(String username,String password,String host,int port,String filePath,String fileName) throws IOException{

		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
	
			JSch jsch = new JSch();
	        Session session = null;
	        try {
	            session = jsch.getSession(username, host, port);
	            session.setConfig("StrictHostKeyChecking", "no");
	            session.setPassword(password);
	            session.connect(); 

	            Channel channel = session.openChannel("sftp");
	            channel.connect();
	            ChannelSftp sftpChannel = (ChannelSftp) channel;
	            try {
	             	File sdCard = Environment.getExternalStorageDirectory();
	            	File dir = new File(sdCard.getAbsolutePath());
	            	File file = new File(dir,fileName);
	            	FileOutputStream f = new FileOutputStream(file);

	            	
	    			
	            	sftpChannel.get(filePath, f);
	            	
	            	OutputStreamWriter myOutWriter = new OutputStreamWriter(f);
	    			myOutWriter.close();
	    			f.close();
	            
	            	
	            	
				
	            	  
				
				} catch (SftpException e) { 
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast.makeText(getApplicationContext(),"khong down dc", Toast.LENGTH_SHORT).show();
					
				
	            sftpChannel.exit();
	            session.disconnect(); 
				}
	        } catch (JSchException e) {
	            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
	            Toast.makeText(getApplicationContext(),"ham ket noi co van de", Toast.LENGTH_SHORT).show();
	            
	        }
	        
	        
	
	}
	
	
	
	
	
	
	
	
	

}
